package com.centaline.androidsalesblog.adapter.viewholder;

import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.CentaDistanceUtil;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centaline.androidsalesblog.widgets.CustomListTagView;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 租售列表
 * Created by guilin on 16/1/26.
 */
public class PostVH extends RecyclerView.ViewHolder {

    private ImageView img_logo, img_360;
    private AppCompatTextView atv_title, atv_tag_fir, atv_tag_sec, atv_tag_thi, atv_price, atv_distance;
    private CustomListTagView customListTagView;

    public PostVH(View itemView) {
        super(itemView);
        img_logo = (ImageView) itemView.findViewById(R.id.img_logo);
        img_360 = (ImageView) itemView.findViewById(R.id.img_360);
        atv_title = (AppCompatTextView) itemView.findViewById(R.id.atv_title);
        atv_tag_fir = (AppCompatTextView) itemView.findViewById(R.id.atv_tag_fir);
        atv_tag_sec = (AppCompatTextView) itemView.findViewById(R.id.atv_tag_sec);
        atv_tag_thi = (AppCompatTextView) itemView.findViewById(R.id.atv_tag_thi);
        atv_price = (AppCompatTextView) itemView.findViewById(R.id.atv_price);
        atv_distance = (AppCompatTextView) itemView.findViewById(R.id.atv_distance);
        customListTagView = (CustomListTagView) itemView.findViewById(R.id.customListTagView);
    }

    /**
     * 二手房
     */
    public void loadSale(DrawableRequestBuilder<String> requestBuilder, int viewType, PostDo postDo, LatLng location) {
        loadSale(requestBuilder, viewType, postDo, location, new ArrayList<String>());
    }

    /**
     * 二手房
     */
    public void loadSale(DrawableRequestBuilder<String> requestBuilder, int viewType, PostDo postDo, LatLng location, ArrayList<String> postIds) {
        GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                .placeHolder(R.drawable.ic_centa_square_logo)
                .error(R.drawable.ic_centa_square_logo)
                .openWifiSwitch()
                .into(img_logo));
        atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %.0f平  %s",
                postDo.getBedroomCount(),
                postDo.getSittingRoomCount(),
                postDo.getSize(),
                TextUtils.isEmpty(postDo.getDirection()) ? "" : postDo.getDirection()));
        atv_tag_thi.setVisibility(View.GONE);
        int titleColor = postIds.contains(postDo.getPostId()) ? Color.parseColor("#FF999999") : Color.parseColor("#FF333333");
        atv_title.setTextColor(titleColor);
        switch (viewType) {
            case 1:
                atv_title.setText(postDo.getCnEstate());
                atv_tag_sec.setText(CentaDistanceUtil.distanceOfPostAndMetro(postDo.getRailLineNames(),
                        postDo.getRailWayName(),
                        postDo.getRailWayDistance()));
                break;
            default:
                atv_title.setText(postDo.getTitle());
                atv_tag_sec.setText(postDo.getCnEstate());
                break;
        }
        img_360.setVisibility(TextUtils.isEmpty(postDo.getTencentVistaUrl()) ? View.GONE : View.VISIBLE);
        atv_price.setText(PriceUtil.salePrice(postDo.getPrice()));
        customListTagView.setTags(postDo.getKeyWords());
        CentaDistanceUtil.distanceView(atv_distance, location, new LatLng(postDo.getLat(), postDo.getLng()));
    }

    /**
     * 租房
     */
    public void loadRent(DrawableRequestBuilder<String> requestBuilder, int viewType, PostDo postDo, LatLng location) {
        loadRent(requestBuilder, viewType, postDo, location, new ArrayList<String>());
    }

    /**
     * 租房
     */
    public void loadRent(DrawableRequestBuilder<String> requestBuilder, int viewType, PostDo postDo, LatLng location, ArrayList<String> postIds) {
        GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                .placeHolder(R.drawable.ic_centa_square_logo)
                .error(R.drawable.ic_centa_square_logo)
                .openWifiSwitch()
                .into(img_logo));
        atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %s",
                postDo.getBedroomCount(),
                postDo.getSittingRoomCount(),
                TextUtils.isEmpty(postDo.getDirection()) ? "" : postDo.getDirection()));
        int titleColor = postIds.contains(postDo.getPostId()) ? Color.parseColor("#FF999999") : Color.parseColor("#FF333333");
        atv_title.setTextColor(titleColor);
        switch (viewType) {
            case 1:
                atv_title.setText(postDo.getCnEstate());
                atv_tag_thi.setText(CentaDistanceUtil.distanceOfPostAndMetro(postDo.getRailLineNames(),
                        postDo.getRailWayName(),
                        postDo.getRailWayDistance()));
                break;
            default:
                atv_title.setText(postDo.getTitle());
                atv_tag_thi.setText(String.format(Locale.CHINA, "%s  %s", postDo.getGscope(), postDo.getCnEstate()));
                break;
        }
        img_360.setVisibility(TextUtils.isEmpty(postDo.getTencentVistaUrl()) ? View.GONE : View.VISIBLE);
        atv_tag_sec.setText(postDo.getFitMent());
        atv_tag_thi.setVisibility(View.VISIBLE);
        customListTagView.setVisibility(View.GONE);
        atv_price.setText(PriceUtil.rentPrice(postDo.getRental()));
        CentaDistanceUtil.distanceView(atv_distance, location, new LatLng(postDo.getLat(), postDo.getLng()));
    }
}
